package com.ypx;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;

public class MapDemo {
    public static void main(String[] args){
        //test0();
        //test1();
        //test2();
        test3();
    }

    private static void test0() {
        Map<String, Double> map = new HashMap<>();
        map.put("张三", 100.0);
        map.put("王五", 300.0);
        map.put("李四", 200.0);
        map.put("赵六", 100.0);
        System.out.println(map);

        map.remove("张三");
        System.out.println(map);
        //判断是否包含李四
        boolean s = map.containsKey("李四");
        System.out.println(s);
        //判断是否包含100分
        boolean t = map.containsValue(100.0);
        System.out.println(t);

        int size = map.size();
        System.out.println(size);
        System.out.println("---------------");
    }
    public static void test1(){
        Map<String,Double> map = new HashMap<>();
        map.put("张三", 100.0);
        map.put("王五", 300.0);
        map.put("赵六", 200.0);
        map.put("李四", 400.0);
        Set<String> keyset = map.keySet();
        //遍历keyset集合
        for(String key : keyset){
            double value = map.get(key);
            System.out.println(key + ":" + value);
        }




    }
    public static void test2() {
        Map<String, Double> map = new HashMap<>();
        map.put("张三", 100.0);
        map.put("王五", 300.0);
        map.put("赵六", 200.0);
        map.put("李四", 400.0);
        //1将map集合转成set集合（将map中的key--value绑定成一个entry对象）
        Set<Map.Entry<String, Double>> entrySet = map.entrySet();
        //2遍历entrySet集合
        for (Map.Entry<String, Double> entry : entrySet) {
            //3.根据entry对象获取key和value
            String key = entry.getKey();
            Double value = entry.getValue();
            System.out.println(key + "-----" + value);

        }

    }
    public static void test3() {
        Map<String, Double> map = new HashMap<>();
        map.put("张三", 100.0);
        map.put("王五", 300.0);
        map.put("赵六", 200.0);
        map.put("李四", 400.0);

        map.forEach(new BiConsumer<String, Double>() {
            @Override
            public void accept(String key, Double value) {
                System.out.println(key + "-----" + value);
            }
        });
        //lambda表达式
        //map.forEach((key, value) -> System.out.println(key + "-----" + value));

    }
}
